<?php

namespace Biz\service\activity\project;

class ExportToolsBizProject
{

    private static ExportToolsBizProject $instance;
    private string $filename = 'hello';
    private array $letterArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    private static array $sheets = [];
    public static function getInstance(): ExportToolsBizProject
    {
        if (empty(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public static function addSheet(array $rows = [], string $title = '')
    {
        self::$sheets[$title] = $rows;
    }
    public static function create(string $filename, array $rows = [], array $merges = [])
    {
        $instance = self::getInstance();
        $instance->filename = $filename;
        $max = 0;
        foreach ($rows as $row) {
            $max = max(count($row), $max);
        }
        $letters = $instance->makePCoordinate($max);
        $excel = new \PHPExcel();
        $activeSheet = $excel->getActiveSheet();
        foreach ($rows as $index => $row) {
            $index ++;
            foreach ($row as $key => $v) {
                $activeSheet->setCellValue($letters[$key] . $index, $v);
            }
        }
//        $img = new \PHPExcel_Worksheet_Drawing();
//        $img->setPath();
        foreach ($merges as $merge) {
            $activeSheet->mergeCells($merge);
        }

        if (self::$sheets) {
            foreach (self::$sheets as $title => $otherData) {
                $max = 0;
                foreach ($otherData as $row) {
                    $max = max(count($row), $max);
                }
                $letters = $instance->makePCoordinate($max);
                $sheet = $excel->createSheet();
                if (strlen($title)) $sheet->setTitle($title);
                foreach ($otherData as $index => $row) {
                    $index ++;
                    foreach ($row as $key => $v) {
                        $sheet->setCellValue($letters[$key] . $index, $v);
                    }
                }
            }
        }


        $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
        self::getInstance()->header();
        $objWriter->save('php://output');
    }

    public function makePCoordinate($len): array
    {
        $base = 26;
        $rows = [];
        $maxLen = $len < $base ? 0 : floor(log($len, $base));
        for ($i = 0; $i <= $len; $i++) {
            $letter = '';
            $loop = true;
            $start = $i;
            for ($j = 0; $j <= $maxLen; $j ++) {
                if ($loop) {
                    $key = $start % $base;
                    $letter = $this->letterArr[$key] . $letter;
                    $start = ($start - $key) / $base;
                    $loop = $start > 0;
                    $start --;
                }
            }
            $rows[] = $letter;
        }
        return $rows;
    }

    public function header()
    {
        ob_end_clean();
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $this->filename . ".xls");
        header("Content-Transfer-Encoding:binary");
    }
}